﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using ContractManager.BLL.Business;
using ContractManager.Lists.Contracts;
using Stimulsoft.Report.Components;

namespace ContractManager.Reports.TendersDocuments
{
    public partial class FrmBc01 : XtraFormBase
    {
        #region "Variable"
        private Entity.Forms _form = new Entity.Forms();
        private bool _allowEdit;
        private bool _onlyView;
        private bool _allowDelete;
        public List<DateTime> ListLockDate { get; set; }

        #endregion

        #region "Form Construction"
        public FrmBc01()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void CheckPer(int formNo)
        {
            var item = ClsForms.SelectByUserForm(ClsVariable.RoleNo, formNo);
            if (item == null) return;

            _allowEdit = item.AllowEdit;
            _allowDelete = item.AllowDelete;
            _form = ClsForms.SelectByPk(formNo);
            this.ListLockDate = ClsVoucherLock.GetLockByFormNo(formNo);
        }

        private void BlankForm()
        {
            
            //txtFromDate.EditValue = null;
            //txtToDate.EditValue = null;
            txtMbId.EditValue = null;

            txtMbId.Properties.DataSource = ClsMemberBranch.SelectAll();
            txtMbId.Properties.DisplayMember = "MbName";
            txtMbId.Properties.ValueMember = "MbID";

            txtContract.Properties.DataSource = ClsContracts.GetLookupAll();
            txtContract.Properties.DisplayMember = "ConNo";
            txtContract.Properties.ValueMember = "ConId";

            //Loai hop dong
            txtConTypId.Properties.DataSource = ClsContractType.SelectAll();
            txtConTypId.Properties.DisplayMember = "ConTypName";
            txtConTypId.Properties.ValueMember = "ConTypId";
            //Loai nguon von

            txtCapId.Properties.DataSource = ClsCapitals.SelectAll();
            txtCapId.Properties.DisplayMember = "CapName";
            txtCapId.Properties.ValueMember = "CapId";
            //Loai hinh cong viec
            txtCatId.Properties.DataSource = ClsCategories.SelectAll();
            txtCatId.Properties.DisplayMember = "CatName";
            txtCatId.Properties.ValueMember = "CatId";

            //Phan loai cong viec
            txtWorkId.Properties.DataSource = ClsWorkType.SelectAll();
            txtWorkId.Properties.DisplayMember = "WorkName";
            txtWorkId.Properties.ValueMember = "WorkId";

            //Chu dau tu
            txtInvID.Properties.DataSource = ClsInvestors.SelectAll();
            txtInvID.Properties.DisplayMember = "InvName";
            txtInvID.Properties.ValueMember = "InvID";

        }

        private void DoPerform()
        {
            string fileName = ReportName;
            string reportName = ClsVariable.GetReportFullPath(fileName);
            string mbId = String.Format("{0}",txtMbId.EditValue);
            string conTypeId = String.Format("{0}", txtConTypId.EditValue);
            string capId = String.Format("{0}", txtCapId.EditValue);
            string catId = String.Format("{0}", txtCatId.EditValue);
            string workId = String.Format("{0}", txtWorkId.EditValue);

            int conId = txtContract.EditValue == null ? 0 : Convert.ToInt32(txtContract.EditValue);
            int invID = txtInvID.EditValue == null ? 0 : Convert.ToInt32(txtInvID.EditValue);

            var report = new Stimulsoft.Report.StiReport();
            report.Load(reportName);

            ClsVariable.CreateWaitDialog(ClsVariable.StrCaption, "Vui lòng đợi...");
            ClsVariable.SetWaitDialogCaption("Đang nạp dữ liệu báo cáo...");

            DataTable dtReport = ClsReports.GetReportBc01(txtDateRange.FromDate, txtDateRange.ToDate,
                                                          mbId, this.StoreProcedureName, conId, conTypeId, capId, catId, workId, invID);
            report.RegData(dtReport);
            report.Compile();
            report.CompiledReport.Click += click;
            ClsVariable.CloseWaitDialog();
            // In thông tin đơn vị, địa chỉ

            report["KyBaoCao"] = txtDateRange.KyBc;

            //report["KyBaoCao"] = String.Format("Từ ngày {0} Đến ngày {1}", txtFromDate.DateTime.ToString("dd/MM/yyyy"),
            //                                   txtToDate.DateTime.ToString("dd/MM/yyyy"));

            report["DoiTuong"] = ClsVariable.IsSelectAllComboBoxEdit(txtMbId)
                ? "Tất cả"
                : String.Format("{0}", txtMbId.Text);


            ClsVariable.ApplyReportParam(report, ClsVariable.DtParam);
            report.Render(true);
            report.Show(this.MdiParent);
        }

        private void click(object sender, EventArgs e)
        {
            StiComponent comp = sender as StiComponent;
            if (comp != null && comp.BookmarkValue == null) return;
            if (comp != null)
            {
                var ma = (string)comp.BookmarkValue;
                if (ma == null) return;
                string[] arr = ma.Split(';');
                int voucherId = Convert.ToInt32(arr[0]);
                const int sysBillStyleId = 0;
                int formNo = Convert.ToInt32(arr[1]);
                int vcate = Convert.ToInt32(arr[2]);

                CheckPer(formNo);
                if (_allowEdit == false) _onlyView = true;
                _allowDelete = false;
                if (String.IsNullOrEmpty(_form.Form_Class)) return;
                if (_form.Form_Class.Contains("FrmContracts"))
                {
                    var frm = new FrmContractsDetail()
                    {
                        MasterId = voucherId,
                        AsSysBillStyleId = sysBillStyleId,
                        AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                        AsPerPersonId = ClsVariable.EmployeeId,
                        FormNo = formNo,
                        FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                        FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                        FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                        FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                        FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                        VoucherCate = vcate,
                        IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                        FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                        FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                        LstLockDate = ListLockDate,
                        AllowDelete = _allowDelete,

                        OnlyView = _onlyView,

                        Text = String.Format("Thông tin {0}", _form.Form_Name),
                        MdiParent = this.MdiParent,

                        WindowState = FormWindowState.Maximized
                    };
                    frm.Show();
                    frm.Focus();
                }

                if (_form.Form_Class.Contains("FrmRevenues"))
                {
                    var frm = new FrmRevenuesDetail()
                    {
                        MasterId = voucherId,
                        AsSysBillStyleId = sysBillStyleId,
                        AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                        AsPerPersonId = ClsVariable.EmployeeId,
                        FormNo = formNo,
                        FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                        FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                        FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                        FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                        FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                        VoucherCate = vcate,
                        IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                        FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                        FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                        LstLockDate = ListLockDate,
                        AllowDelete = _allowDelete,

                        OnlyView = _onlyView,

                        Text = String.Format("Thông tin {0}", _form.Form_Name),
                        MdiParent = this.MdiParent,

                        WindowState = FormWindowState.Maximized
                    };
                    frm.Show();
                    frm.Focus();
                }

                if (_form.Form_Class.Contains("FrmDocuments"))
                {
                    var frm = new FrmDocumentsDetail()
                    {
                        MasterId = voucherId,
                        AsSysBillStyleId = sysBillStyleId,
                        AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                        AsPerPersonId = ClsVariable.EmployeeId,
                        FormNo = formNo,
                        FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                        FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                        FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                        FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                        FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                        VoucherCate = vcate,
                        IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                        FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                        FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                        LstLockDate = ListLockDate,
                        AllowDelete = _allowDelete,

                        OnlyView = _onlyView,

                        Text = String.Format("Thông tin {0}", _form.Form_Name),
                        MdiParent = this.MdiParent,

                        WindowState = FormWindowState.Maximized
                    };
                    frm.Show();
                    frm.Focus();
                }

                if (_form.Form_Class.Contains("FrmTenderDocuments"))
                {
                    var frm = new FrmTenderDocumentsDetail()
                    {
                        MasterId = voucherId,
                        AsSysBillStyleId = sysBillStyleId,
                        AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                        AsPerPersonId = ClsVariable.EmployeeId,
                        FormNo = formNo,
                        FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                        FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                        FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                        FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                        FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                        VoucherCate = vcate,
                        IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                        FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                        FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                        LstLockDate = ListLockDate,
                        AllowDelete = _allowDelete,

                        OnlyView = _onlyView,

                        Text = String.Format("Thông tin {0}", _form.Form_Name),
                        MdiParent = this.MdiParent,

                        WindowState = FormWindowState.Maximized
                    };
                    frm.Show();
                    frm.Focus();
                }
            }
        }
        #endregion

        #region "Form Event"
        private void FrmBc01_Load(object sender, EventArgs e)
        {
            txtDateRange.Year = ClsVariable.Year;
            txtDateRange.SelectIndex = 3;
            BlankForm();
            if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            {
                var fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
                layoutControl.RestoreLayoutFromXml(fileLayout);
            }
        }
        #endregion


        #region "Button Event"
        private void btnOk_Click(object sender, EventArgs e)
        {
            DoPerform();

        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        

    }
}
